package com.hz.sdka.ada;

import android.app.Activity;
import android.text.TextUtils;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.anythink.banner.api.ATBannerListener;
import com.anythink.banner.api.ATBannerView;
import com.anythink.core.api.ATAdInfo;
import com.anythink.core.api.AdError;
import com.hz.sdka.commona.base.BaseAd;
import com.hz.sdka.commona.bean.HZAdError;
import com.hz.sdka.commona.type.HZAdType;

public final class AdBannera extends BaseAd {
    private String TAG = "[banner] ===>";

    private ATBannerView atBannerView;
    private AdListenera adListener;


    public AdBannera(Activity activity, String placeId, String scenario, ViewGroup frameLayout) {
        super(placeId, HZAdType.INTERSTITIAL.indexOf());
        this.atBannerView = new ATBannerView(activity);
        this.atBannerView.setPlacementId(placeId);
        if (!TextUtils.isEmpty(scenario)) atBannerView.setScenario(scenario);
        int width = activity.getResources().getDisplayMetrics().widthPixels;//定一个宽度值，比如屏幕宽度
        int height = ViewGroup.LayoutParams.WRAP_CONTENT;

        //如果出现Banner有时高、有时低的情况，请使用此代码
//        float ratio = 320 / 90f;//必须跟TopOn后台配置的Banner广告源宽高比例一致，假设尺寸为320x50
//        int width = activity.getResources().getDisplayMetrics().widthPixels;//定一个宽度值，比如屏幕宽度
//        int height = (int) (width / ratio);
//        Map<String, Object> localMap = new HashMap<>();
//        localMap.put(ATAdConst.KEY.AD_WIDTH, width);
//        localMap.put(ATAdConst.KEY.AD_HEIGHT, height);
//        atBannerView.setLocalExtra(localMap);
        atBannerView.setLayoutParams(new FrameLayout.LayoutParams(width, height));
        frameLayout.addView(atBannerView);
        this.atBannerView.setBannerAdListener(new ATBannerListener() {
            @Override
            public void onBannerLoaded() {
                if (adListener != null) adListener.adLoaded();
            }

            @Override
            public void onBannerFailed(AdError adError) {
                //注意：禁止在此回调中执行广告的加载方法进行重试，否则会引起很多无用请求且可能会导致应用卡顿
                //AdError，请参考 https://docs.toponad.com/#/zh-cn/android/android_doc/android_test?id=aderror
                Log.e(TAG, "onBannerFailed:" + adError.getFullErrorInfo());
                if (adListener != null)
                    adListener.adError(new HZAdError(adError.getCode(), adError.getDesc(),
                            adError.getPlatformCode(),
                            adError.getPlatformMSG()));
            }

            @Override
            public void onBannerClicked(ATAdInfo atAdInfo) {
            }

            @Override
            public void onBannerShow(ATAdInfo atAdInfo) {
                if (adListener != null) adListener.adShow();
                //ATAdInfo可区分广告平台以及获取广告平台的广告位ID等
                //请参考 https://docs.toponad.com/#/zh-cn/android/android_doc/android_sdk_callback_access?id=callback_info

            }

            @Override
            public void onBannerClose(ATAdInfo atAdInfo) {
                if (atBannerView != null && atBannerView.getParent() != null) {
                    ((ViewGroup) atBannerView.getParent()).removeView(atBannerView);
                }
                if (adListener != null) adListener.adClose();
            }

            @Override
            public void onBannerAutoRefreshed(ATAdInfo atAdInfo) {

            }

            @Override
            public void onBannerAutoRefreshFail(AdError adError) {
                //AdError：https://docs.toponad.com/#/zh-cn/android/android_doc/android_test?id=aderror
                Log.e(TAG, "onBannerAutoRefreshFail:" + adError.getFullErrorInfo());
            }
        });
    }


    @Override
    public void load() {
        if (adListener != null) {
            adListener.adRequest();
            adListener.adLoading();
        }
        if (atBannerView != null)
            atBannerView.loadAd();
    }


    public void setListener(AdListenera listener) {
        this.adListener = listener;
    }


    /**
     * 获取广告价格
     */
    public double getEcpm() {
        if (atBannerView != null) {
            return atBannerView.checkAdStatus().getATTopAdInfo().getEcpm();
        }
        return 0.00;
    }

    public void onDestroy() {
        if (atBannerView != null)
            atBannerView.destroy();
    }
}
